﻿using System;
using JRCore.protocol;

namespace WHHXMQ.HXMQutil
{
    public  class TDUtil
    {
        public delegate void Log(string txt);
        public event Log LogEvent;

        //获取MQ消息要求的过车信息格式
        public static string MakeMqMsg_KK(PicCenterPush mqparam)
        {
            try
            {
                string uuid = System.Guid.NewGuid().ToString();//唯一标识
                string chedaoBH = TDUtil.GetLaneNum(mqparam.P17);//车道编号
                string VioTime = TDUtil.GetMqVioTime(mqparam.P18);//经过时间 YYYY-MM-DD HH24:MM:SS
                string RoadDirection = TDUtil.GetRoadDirection(mqparam.P16);//方向编号
                string CarColor = TDUtil.GetCarColor(mqparam.P24);//车身颜色
                string CarCPZB = GetCarPlateXY(mqparam.P46);//例：819/1506/122/26/1 号牌特征图片信息
                string PlateColor = TDUtil.GetCarPlateColor(mqparam.P22);
                string PassTime = TDUtil.GetNowTime();
                return string.Format("VMKS,2.2,0002,{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16},{17},{18}, {19}, {20}, {21}",
                    uuid, "03", mqparam.P23, mqparam.P21, mqparam.P13, mqparam.P15, "1", mqparam.P8, chedaoBH, mqparam.P27,
                    VioTime, mqparam.P12, RoadDirection, mqparam.P37, mqparam.P38, mqparam.P39, CarColor, GetCarCLPP(mqparam.P45), "01", CarCPZB,
                    PlateColor, PassTime);
            }
            catch (Exception er)
            {
                //LogEvent("MQ Resume()异常" + er.Message);
                LogHelper.WriteLog(typeof(TDUtil), er, "MQ Resume()异常" + er.Message);
                return "";
            }
        }

        //获取MQ消息要求的超速违法信息格式
        public static string MakeMqMsg_VIO(PicCenterPush mqparam)
        {
            try
            {
                string uuid = System.Guid.NewGuid().ToString();//唯一标识
                string chedaoBH = TDUtil.GetLaneNum(mqparam.P7);//车道编号
                string VioTime = TDUtil.GetMqVioTime(mqparam.P18);//经过时间 YYYY-MM-DD HH24:MM:SS
                string RoadDirection = TDUtil.GetRoadDirection(mqparam.P16);//方向编号
                string CarColor = TDUtil.GetCarColor(mqparam.P24);//车身颜色
                string CarCPZB = GetCarPlateXY(mqparam.P46);//例：819/1506/122/26/1 号牌特征图片信息
                string PlateColor = TDUtil.GetCarPlateColor(mqparam.P22);
                string PassTime = TDUtil.GetNowTime();
                return string.Format("ILLEGAL02,2.4,0002,{0},3,{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16},{17},{18},{19},{20},{21},{22},{23},{24},{25},{26},{27},{28}",
                    uuid, mqparam.P21, mqparam.P23, VioTime,mqparam.P10,mqparam.P11, mqparam.P15,mqparam.P12, "02","1", mqparam.P8,RoadDirection,
                    chedaoBH, mqparam.P27,mqparam.P26,mqparam.P26,mqparam.P26,mqparam.P26,mqparam.P26,mqparam.P26,mqparam.P26,
                    mqparam.P30, mqparam.P31, mqparam.P32, "", CarCPZB, PlateColor, PassTime, "0");
            }
            catch (Exception er)
            {
                //TransferForm.m_log.ErrorFormat("MakeMqMsg 异常:{0}", er.Message);
                LogHelper.WriteLog(typeof(TDUtil), er, "MakeMqMsg 异常:{0}" + er.Message);

                return "";
            }
        }

       //获取车辆品牌编号
        public static string GetCarCLPP(string clpp)
        {
            string strClppId = "";
            switch (clpp)
            {
                case "Unknown"://"Unknown"未知 
                    strClppId = "";
                    break;
                case "Audi"://"Audi" 奥迪 0
                    strClppId = "0";
                    break;
                case "Honda"://"Honda" 本田 4
                    strClppId = "4";
                    break;
                case "Buick"://"Buick" 别克 8
                    strClppId = "8";
                    break;
                case "Volkswagen"://"Volkswagen" 大众 5
                    strClppId = "5";
                    break;
                case "Toyota"://"Toyota" 丰田 7
                    strClppId = "7";
                    break;
                case "BMW": //"BMW" 宝马 1
                    strClppId = "1";
                    break;
                case "Peugeot"://"Peugeot" 标致 11
                    strClppId = "11";
                    break;
                case "Ford"://"Ford" 福特 12
                    strClppId = "12";
                    break;
                case "Mazda": //"Mazda" 马自达 6
                    strClppId = "6";
                    break;
                case "Nissan"://"Nissan" 尼桑 14
                    strClppId = "14";
                    break;
                case "Hyundai"://"Hyundai" 现代 2
                    strClppId = "2";
                    break;
                case "Suzuki"://"Suzuki" 铃木 21
                    strClppId = "21";
                    break;
                case "Citroen"://"Citroen" 雪铁龙 10
                    strClppId = "10";
                    break;
                case "Benz"://"Benz" 奔驰 3
                    strClppId = "3";
                    break;
                case "BYD"://"BYD" 比亚迪 16
                    strClppId = "16";
                    break;
                case "Geely"://"Geely" 吉利 33
                    strClppId = "33";
                    break;
                case "Lexus"://"Lexus" 雷克萨斯 13
                    strClppId = "13";
                    break;
                case "Chevrolet"://"Chevrolet" 雪佛兰 9
                    strClppId = "9";
                    break;
                case "Chery"://"Chery" 奇瑞 15
                    strClppId = "15";
                    break;
                case "Kia"://"Kia" 起亚 17
                    strClppId = "17";
                    break;
                case "Charade"://"Charade" 夏利 78
                    strClppId = "18";
                    break;
                case "DF"://"DF" 东风 72
                    strClppId = "72";
                    break;
                case "Naveco"://"Naveco" 依维柯 80
                    strClppId = "80";
                    break;
                case "SGMW"://"SGMW" 五菱 75
                    strClppId = "75";
                    break;
                case "Jinbei"://"Jinbei" 金杯 30
                    strClppId = "30";
                    break;
            }

            return strClppId;
        }

        //获取车牌坐标格式
        public static string GetCarPlateXY(string strXY)
        {
            //left，top，right，bottom
            //"819/1506/122/26/1";//x/y/w/h/n，
            string strNewXY = "";
            string[] arr = strXY.Split(',');
            if (arr.Length==4)
            {
                int left = Convert.ToInt32(arr[0]);
                int top = Convert.ToInt32(arr[1]);
                int right = Convert.ToInt32(arr[2]);
                int bottom = Convert.ToInt32(arr[3]);
                int width = right - left;
                int height = bottom - top;
                strNewXY = string.Format("{0}/{1}/{2}/{3}/1", arr[0], arr[1], width, height);
            }
            return strNewXY;

        }

        //获取车道编号
        public static string GetLaneNum(string lanNum) 
        {
            if (lanNum.Length == 1)
            {
                return "0" + lanNum;
            }

            return lanNum;
        }
        //获取运维需要的标准时间
        public static string GetMqVioTime(string strVioTime)
        {
            if (strVioTime.Length >= 14)
            {
                return string.Format("{0}-{1}-{2} {3}:{4}:{5}", strVioTime.Substring(0, 4),
                    strVioTime.Substring(4, 2), strVioTime.Substring(6, 2), strVioTime.Substring(8, 2),
                    strVioTime.Substring(10, 2), strVioTime.Substring(12, 2));
            }
            else
            {
                DateTime dt = DateTime.Now;
                return string.Format("{0}-{1}-{2} {3}:{4}:{5}", dt.Year, GetFullNumber(dt.Month),
                    GetFullNumber(dt.Day), GetFullNumber(dt.Hour), GetFullNumber(dt.Minute),
                    GetFullNumber(dt.Second));
            }
        }

        //获取运维需要的标准时间
        public static string GetNowTime()
        {
            DateTime dt = DateTime.Now;
            return string.Format("{0}-{1}-{2} {3}:{4}:{5}", dt.Year, GetFullNumber(dt.Month),
                GetFullNumber(dt.Day), GetFullNumber(dt.Hour), GetFullNumber(dt.Minute),
                GetFullNumber(dt.Second));
        }

        //获取标准路口方向
        public static string GetRoadDirection(string strDirect)
        {
            if (strDirect == "2")//原来代表由南向北
            {
                return "3";
            }
            else if (strDirect == "3") //原来代表由东向西
            {
                return "2";
            }

            return strDirect;
        }

        //获取标准的车身颜色
        public static string GetCarColor(string strColor)
        {
            //0-其他色 1-白色 2-银色 3-灰色 4-黑色 5-红色 6-深蓝7-蓝色 8-黄色
            //9-绿色 10-棕色 11-粉色 12-紫色，没有为空字符串
            //A	白
            //B	灰
            //C	黄
            //D	粉
            //E	红
            //F	紫
            //G	绿
            //H	蓝
            //I	棕
            //J	黑
            //Z	其他

            string strBZColor = "Z";
            switch (strColor)
            {
                case "0":
                    strBZColor = "Z";
                    break;
                case "1":
                    strBZColor = "A";
                    break;
                case "3":
                    strBZColor = "B";
                    break;
                case "4":
                    strBZColor = "J";
                    break;
                case "5":
                    strBZColor = "E";
                    break;
                case "6":
                    strBZColor = "H";
                    break;
                case "7":
                    strBZColor = "H";
                    break;
                case "8":
                    strBZColor = "C";
                    break;
                case "9":
                    strBZColor = "G";
                    break;
                case "10":
                    strBZColor = "I";
                    break;
            }

            return strBZColor;
        }

        //获取车牌颜色
        public static string GetCarPlateColor(string strPlateColor)
        {
            //蓝 黄 白 黑
            //0白;1黄;2蓝;3黑;4其他
            string strNewColor = "4";
            switch (strPlateColor)
            {
                case "蓝":
                    strNewColor = "2";
                    break;
                case "黄":
                    strNewColor = "1";
                    break;
                case "白":
                    strNewColor = "0";
                    break;
                case "黑":
                    strNewColor = "3";
                    break;
            }

            return strNewColor;
        }

        //数字小于10补0
        public static string GetFullNumber(int nNumber)
        {
            if (nNumber < 10)
            {
                return string.Format("0{0}", nNumber);
            }
            else
            {
                return Convert.ToString(nNumber);
            }
        }
    }
}
